﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace centraldba_dbrestore_utility
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        bool isShuttingDown = false;

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // Automatically upgrade user settings if needed
            //  See:  http://www.ngpixel.com/2011/05/05/c-keep-user-settings-between-versions/
            if (centraldba_dbrestore_utility.Properties.Settings.Default.UpdateSettings)
            {
                centraldba_dbrestore_utility.Properties.Settings.Default.Upgrade();
                centraldba_dbrestore_utility.Properties.Settings.Default.UpdateSettings = false;
                centraldba_dbrestore_utility.Properties.Settings.Default.Save();
            }

            // Ensure that the MSX Server has been set before starting
            if (centraldba_dbrestore_utility.Properties.Settings.Default.centraldba_server_name.Equals("yourserver"))
            {
                App.Current.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;

                frmConfigure cfg = new frmConfigure();
                cfg.ShowDialog();
            }

            // Start the Application
            if (!isShuttingDown)
            {
                MainWindow wnd = new MainWindow();
                App.Current.MainWindow = wnd;

                if (App.Current.ShutdownMode != System.Windows.ShutdownMode.OnLastWindowClose)
                    App.Current.ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;

                App.Current.MainWindow.Show();
            }
        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            isShuttingDown = true;
        }
    }
}
